<script>
import * as api from '@/api/customer/tag'
import * as apiI from '@/api/customer'

export default {
  name: 'AddTag',
  components: {},
  props: {
    // 添加标签显隐
    visible: {
      type: Boolean,
      default: false,
    },
    // 表单参数
    form: {
      type: Object,
      // default: () => ({
      //   forbidTypeS: 0
      // }),
    },
  },
  emits: ['update:visible'],
  data() {
    return {
      // 添加标签输入框
      newInput: '',
      // 表单验证规则
      rules: Object.freeze({
        forbidType: [{ required: true, message: '必填项', trigger: 'blur' }],
        reason: [{ required: true, message: '必填项', trigger: 'blur' }],
      }),
      // 添加便签按钮显隐
      visibleAdd: false,
      value2: '',
      pickerOptions: {
          shortcuts: [{
            text: '今天',
            onClick(picker) {
              picker.$emit('pick', new Date());
            }
          }, {
            text: '昨天',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24);
              picker.$emit('pick', date);
            }
          }, {
            text: '一周前',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', date);
            }
          }]
        },
    }
  },
  watch: {},
  computed: {
    Pvisible: {
      get() {
        if (this.visible) {
          this.$nextTick(() => {
            this.$refs['form'].clearValidate()
          })
        }
        return this.visible
      },
      set(val) {
        this.$emit('update:visible', val)
      },
    },
  },
  created() {
  },
  mounted() {},
  methods: {
    setChange(type) {
      // if (type !== 3) {
      //   delete this.form.typeSub
      //   this.form.otherContent = []
      // }
    },
    closeTag(tag, index) {
      // if (tag.id) {
      //   tag.status = 1
      //   // this.$forceUpdate()
      // } else {
      this.form.weTags.splice(index, 1)
      // }
    },

    showInput() {
      this.visibleAdd = true
      this.$nextTick((_) => {
        this.$refs.saveTagInput.$refs.input.focus()
      })
    },
    newInputConfirm() {
      let name = this.newInput
      if (name) {
        Array.isArray(this.form.weTags) || (this.form.weTags = [])
        let isExist = this.form.weTags.some((e) => {
          return e.name === name
        })
        if (isExist) {
          this.msgError('标签名已存在，不可重复添加')
          return
        }
        this.form.weTags.push({ name })
      }
      this.visibleAdd = false
      this.newInput = ''
    },
    submit() {
      console.log('111111')
      this.$refs['form'].validate((valid) => {
          let form = JSON.parse(JSON.stringify(this.form))
          console.log('form ========= ', form)
          if (!form.reason.length) {
            return
          }
          // if ('' == form.forbidType || null == form.forbidType || undefined == form.forbidType) {
          //   return
          // }
          form.status = '1'
          apiI.handleUserComplian(form).then(() => {
            this.msgSuccess('操作成功')
            this.Pvisible = false
            this.$emit('success')
          })
        })
    },
  },
}
</script>

<template>
  <el-dialog
    :title="(form.groupId ? '修改' : '添加') + '标签/组1'"
    :model-value="Pvisible"
    @update:modelValue="(val) => (Pvisible = val)"
    width="500px"
    append-to-body
    :close-on-click-modal="false">
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="原因" prop="reason">
        <el-input
          v-model.trim="form.reason"
          maxlength="15"
          show-word-limit
          type='textarea'
          placeholder="请输入封禁原因" />
      </el-form-item>
      <!-- <el-form-item label="标签" prop="weTags">
        <template v-for="(item, index) in form.weTags" :key="index">
          <el-tag v-if="item.status !== 1" type="primary" closable @close="closeTag(item, index)">
            {{ item.name }}
          </el-tag>
        </template>
        <div>
          <el-input
            class="input-new-tag"
            v-if="visibleAdd"
            v-model.trim="newInput"
            ref="saveTagInput"
            maxlength="10"
            show-word-limit
            @keyup.enter="newInputConfirm"
            @blur="newInputConfirm"></el-input>
          <el-button v-else type="primary" plain class="button-new-tag" @click="showInput">添加标签</el-button>
        </div>
      </el-form-item> -->
      <el-form-item label="类型" prop="forbidType">
        <el-radio-group v-model="form.forbidType" @change="setChange">
          <el-radio :label="0">群聊功能封禁</el-radio>
          <el-radio :label="1">群聊、私聊功能封禁</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="submit" v-preventReClick="1000">确 定</el-button>
        <el-button @click="Pvisible = false">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<style lang="scss" scoped>
.input-new-tag {
  width: auto;
  margin-right: 10px;
  vertical-align: bottom;
}
</style>
